package com.ny20231079080.dao.Impl;

import com.ny20231079080.dao.ProductDao;
import com.ny20231079080.entity.Products;
import homework.six.DbUntil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ProductDaoImpl implements ProductDao {
    DbUntil dbUntil = new DbUntil();

    @Override
    public Products findById(int productId) throws SQLException, ClassNotFoundException {
        Connection con = dbUntil.getCon();
        String sql = "SELECT * FROM products WHERE product_id = ?";
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setInt(1, productId);
        ResultSet rs = ps.executeQuery();

        if (rs.next()) {
            Products product = new Products();
            product.setProduct_id(rs.getInt("product_id"));
            product.setName(rs.getString("name"));
            product.setPrice(rs.getDouble("price"));
            product.setStock(rs.getInt("stock"));
            product.setCategory(rs.getString("category"));
            product.setDescription(rs.getString("description"));
            return product;
        }

        return null;
    }

    @Override
    public List<Products> findAllProducts() throws SQLException, ClassNotFoundException {
        Connection con = dbUntil.getCon();
        String sql = "select * from products";
        PreparedStatement preparedStatement = con.prepareStatement(sql);
        ResultSet resultSet = preparedStatement.executeQuery();
        List<Products> productsList = new ArrayList<Products>();
        while (resultSet.next()) {
            Products products = new Products();
            int id = resultSet.getInt("product_id");
            products.setProduct_id(id);
            products.setName(resultSet.getString("name"));
            products.setPrice(resultSet.getDouble("price"));
            products.setStock(resultSet.getInt("stock"));
            products.setCategory(resultSet.getString("category"));
            products.setDescription(resultSet.getString("description"));
            productsList.add(products);
        }
        return productsList;
    }

    @Override
    public int insert(Products product) throws SQLException, ClassNotFoundException{
        Connection con = dbUntil.getCon();
        String sql = "INSERT INTO products(name, price, stock, category, description) VALUES (?, ?, ?, ?, ?)";
        PreparedStatement ps = con.prepareStatement(sql);

        ps.setString(1, product.getName());
        ps.setDouble(2, product.getPrice());
        ps.setInt(3, product.getStock());
        ps.setString(4, product.getCategory());
        ps.setString(5, product.getDescription());

        return ps.executeUpdate();
    }

    @Override
    public int update(Products product) throws SQLException, ClassNotFoundException {
        Connection con = dbUntil.getCon();
        String sql = "UPDATE products SET name=?, price=?, stock=?, category=?, description=? WHERE product_id=?";
        PreparedStatement ps = con.prepareStatement(sql);

        ps.setString(1, product.getName());
        ps.setDouble(2, product.getPrice());
        ps.setInt(3, product.getStock());
        ps.setString(4, product.getCategory());
        ps.setString(5, product.getDescription());
        ps.setInt(6, product.getProduct_id());

        return ps.executeUpdate();
    }

    @Override
    public int delete(int productId)throws SQLException, ClassNotFoundException {
        Connection con = dbUntil.getCon();
        String sql = "DELETE FROM products WHERE product_id = ?";
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setInt(1, productId);
        return ps.executeUpdate();
    }


}


